import cv2
import os
import numpy as np

# g(x,y) = f(x,y)h(x,y)

srcImgPath = os.getcwd().strip('code\ch1') + '\pic\DIP3E_Original_Images_CH02\Fig0229(a)(tungsten_filament_shaded).tif'
mulImgPath = os.getcwd().strip('code\ch1') + '\pic\DIP3E_Original_Images_CH02\Fig0229(b)(tungsten_sensor_shading).tif'
# print(srcImgPath)
# g(x,y)
srcImg = cv2.imread(srcImgPath)
srcImgF = srcImg.astype(np.float32)
# h(x,y)
mulSrcImg = cv2.imread(mulImgPath)
mulSrcImgF = mulSrcImg.astype(np.float32)

mulImgF = np.zeros(srcImg.shape, np.float32)

cv2.divide(np.ones(srcImg.shape, np.float32), mulSrcImgF, mulImgF)

resultF = np.zeros(srcImg.shape, np.float32)
# 图像取倒数
cv2.multiply(srcImgF, mulImgF, resultF)

# 溢出处理
# resultF = np.clip(resultF, 0, 255)
# result = resultF.astype(np.uint8)


cv2.imshow('src', srcImg)
cv2.imshow('result', resultF)
cv2.waitKey(0)

# 测试float to uint8
A = np.array([259.0, 239.0, 258.0, 258.0, 259.0], np.float32)
B = np.clip(A, 0, 255)

print(A)
print(B.astype(np.uint8))